<?php
/**
 * Author: LaiGuiming
 * Time: 2017-06-26 16:18
 * Copyright (C) 2016 Longyan Wehome Message CO.,LTD.
 */

namespace app\admin\controller\sys;
use app\common\controller\BackEnd;
use app\admin\model\Node;

/**
 * 菜单控制器
 * Class AuthRule
 * @package app\admin\controller
 */
class Menu extends BackEnd
{
    private $model;
    protected $modelName;//模型名称
    protected $explain;//说明

    public function _initialize()
    {
        parent::_initialize();
        $this->model=D("menu");
        $this->modelName="Menu";
        $this->explain="菜单";
    }

    /**
     * 列表
     * @return mixed
     */
    public function index()
    {
        return $this->fetch();
    }

    public function getMenus($type=0,$checks=""){
        $data=$this->model->where("status!=2")->order('sort asc')->select();
        $nodes=$this->treeInit($data,$checks);

        foreach ($data as $temp){
            $node=$nodes[$temp['id']];
            $pid=$node->getParentId();
            if($pid!=0){
                $pnode=$nodes[$node->getParentId()];
                $pc=$pnode->getChildren();
                array_push($pc,$node);
                $pnode->setChildren($pc);
            }
        }

        $resData="[";
        foreach ($data as $temp){
            $node=$nodes[$temp['id']];
            $pid=$node->getParentId();
            if($pid==0){//根节点
                if($type==0){
                    $resData=$resData.$node->toString1().",";
                }else{
                    $resData=$resData.$node->toString2().",";
                }
            }
        }
        //切割掉最后一个逗号
        $resData = substr($resData, 0, strlen($resData) - 1);

        $resData=$resData."]";
        echo $resData;
    }

    public function treeInit($data,$checks){
        $nodes=array();
        $ids=array();
        if($checks!=""){
            $ids = explode(',',$checks);
        }

        foreach ($data as $vo){
            $node=new Node();
            if(in_array($vo['id'],$ids)){
                $node->setChecked("true");
            }
            $node->setId($vo['id']);
            $node->setText($vo['name']);
            $node->setParentId($vo['pid']);
            $node->setIcon($vo['icon']);
            $node->setType($vo['type']);
            $node->setHref($vo['code']);
            $nodes[$vo['id']]=$node;
        }
        return $nodes;
    }

    /**
     * 新增
     * @param int $id
     * @param string $pid
     * @return mixed
     */
    public function add($id = -1,$pid="")
    {
        $this->assign('data', $this->model->get($id));
        $this->assign('pid', $pid);
        return $this->fetch();
    }

    /**
     * 保存
     * @param string $id
     * @return string|void
     */
    public function save($id = '')
    {
        //校验
        $validate=validate($this->modelName);
        if(!$validate->check($_POST)){
            $this->resError($validate->getError());
            return;
        }

        if ($id != '') {//修改
            $result = $this->model->allowField(true)->save($_POST,['id' => $id]);
            $this->saveLog("修改菜单【".$_POST['name']."】");
        }else{
            $result =$this->model->allowField(true)->save($_POST);
            $this->saveLog("新增菜单【".$_POST['name']."】");
        }
        //更新缓存
        session("menus", getAuth());
        $this->resData($result);
    }

    /**
     * 删除
     * @param string $id
     */
    public function del($id = '')
    {
        //递归删除自己及子菜单
        $this->delCascade($id);
        $this->saveLog("删除菜单【".$id."】");
        //更新缓存
        session("menus", getAuth());
        $this->resSuccess();
    }

    /**
     * 递归删除自己及子菜单
     * @param $id
     */
    public function delCascade($id){
        $this->model->where('id in ('.$id.')')->update([ 'status' => 2]);
        $children=$this->model->where("pid=".$id)->select();
        if(count($children)>0){
            foreach($children as $temp){
                $this->delCascade($temp['id']);
            }
        }
    }

    /**
     * 根据等级获取菜单
     * @param int $level
     */
    public function getByLevel($level = 1)
    {
        $rules = $this->model->where('level=' . $level)->field('id,name')->select();
        $this->resSuccess($rules);
    }
}